
#include <rtthread.h>
#include "board.h"

/**
  * @brief  配置TIM16复用输出PWM时用到的I/O
  * @param  无
  * @retval 无
  */
static void TIM16_GPIO_Config(void)
{
    rcu_periph_clock_enable(BEEP_GPIO_CLK);
    gpio_mode_set(BEEP_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, BEEP_GPIO_PIN);
    gpio_output_options_set(BEEP_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,BEEP_GPIO_PIN);
    gpio_af_set(BEEP_GPIO_PORT, TIM16_CH0_AF, BEEP_GPIO_PIN);
}

/**
  * @brief  配置TIM16在输出PWM情况下的工作模式
  * @param  无
  * @retval 无
  */
static void TIM16_PWMOUTPUT_Config(void)
{
    /* TIMER16 configuration: generate PWM signals with different duty cycles:
       SystemCoreClock=84000000
       TIMER16CLK = SystemCoreClock / 84 = 1MHz */
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(BEEP_TIM_CLK);
    timer_deinit(BEEP_TIM);

    /* TIMER2 configuration */
    timer_initpara.prescaler         = (84-1);
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 370;  //370us的周期
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(BEEP_TIM,&timer_initpara);

     /* CH0 configuration in PWM mode */
    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    timer_channel_output_config(BEEP_TIM,TIMER_CH_0,&timer_ocintpara);

    timer_channel_output_pulse_value_config(BEEP_TIM,TIMER_CH_0,0);
    timer_channel_output_mode_config(BEEP_TIM,TIMER_CH_0,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(BEEP_TIM,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(BEEP_TIM);
    if(ConfigParam.SoundMode.sound_status==BUZZER_ON)
    {
      timer_primary_output_config(BEEP_TIM,ENABLE);
      timer_enable(BEEP_TIM);
    }
    else
    {
      timer_primary_output_config(BEEP_TIM,DISABLE);
      timer_disable(BEEP_TIM);
    }
}

  // 失能定时器

/**
  * @brief  初始化通用控制定时器定时
  * @param  无
  * @retval 无
  */
void TIM16_Buzzer_Configuration(void)
{
  TIM16_GPIO_Config();
  TIM16_PWMOUTPUT_Config();
}

/**
  * @brief  控制蜂鸣器的开关
  * @param  En:是否开启蜂鸣器
  *         ENABLE:开启
  *         DISABLE:关闭
  * @retval 无
  */
static void BuzzerControl(ControlStatus En)
{
    if(En==ENABLE)
    {
      timer_channel_output_pulse_value_config(BEEP_TIM, TIMER_CH_0,185);
    }
    else
    {
      timer_channel_output_pulse_value_config(BEEP_TIM, TIMER_CH_0,0);
    }
}


/**
  * @brief  开100ms
  * @param  无
  * @retval 无
  */
static void BeepOnce(void)
{
  BuzzerControl(ENABLE);
  rt_thread_delay(100);
  BuzzerControl(DISABLE);
}


/**
  * @brief  开60ms,关15ms,循环3次
  * @param  无
  * @retval 无
  */
static void BeepThreeTimes(void)
{
  BuzzerControl(ENABLE);
  rt_thread_delay(60);
  BuzzerControl(DISABLE);
  rt_thread_delay(15);
  BuzzerControl(ENABLE);
  rt_thread_delay(60);
  BuzzerControl(DISABLE);
  rt_thread_delay(15);
  BuzzerControl(ENABLE);
  rt_thread_delay(60);
  BuzzerControl(DISABLE);
  // rt_thread_delay(15);
}

uint8_t Buzzer_Sta=0;

/**
  * @brief  蜂鸣器动作函数,在蜂鸣器线程中调用
  * @param  无
  * @retval 无
  */
void BuzzerAction(void)
{  //endeavour
  rt_err_t result = RT_EOK;
  uint8_t *p = NULL;

  result = rt_mb_recv( buzzer_mail,                //邮箱的句柄
                        (rt_uint32_t*)&p,    //转换成rt_uint32_t型的指针(这种写法很怪呀)
                        RT_WAITING_FOREVER);        //一直等直到收到消息
  if(result==RT_EOK)
  {
    if(*p==1)
    {
      BeepOnce();
    }
    else if(*p==3)
    {
      BeepThreeTimes();
    }
  }
}